package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.activity.OrderEvaluateActivity;
import com.avic.rong.userOrder.bean.AllOrderListBean;
import com.avic.rong.utils.GlideUtils;
import com.example.minato.minastore.utils.AppUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

/**
 * Created by XinEr on 2019/9/24.
 * Describe:
 */
public class MoreEvaluateListAdapter extends RecyclerView.Adapter<MoreEvaluateListAdapter.ViewHolder> {
    private Context context;
    private List<AllOrderListBean.OrderListBean.ItemsViewBean> itemsView;
    private AllOrderListBean.OrderListBean orderListBean;

    public AllOrderListBean.OrderListBean getOrderListBean() {
        return orderListBean;
    }

    public void setOrderListBean(AllOrderListBean.OrderListBean orderListBean) {
        this.orderListBean = orderListBean;
    }

    public MoreEvaluateListAdapter(Context context, List<AllOrderListBean.OrderListBean.ItemsViewBean> itemsView) {
        this.context = context;
        this.itemsView=itemsView;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.more_evaluate_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {


        if (orderListBean==null)return;
        AllOrderListBean.OrderListBean.ItemsViewBean itemsViewBean = itemsView.get(i);

        if (orderListBean.getOrderType() == 4) {
            if (!TextUtils.isEmpty(itemsViewBean.getGoodsImg())) {
                if (itemsViewBean.getGoodsImg().contains("rongrong.")) {
                    GlideUtils.loaderHanldeRoundImage(context, itemsViewBean.getGoodsImg(),viewHolder. iv_goods_pic,10);
                } else {
                    GlideUtils.loaderHanldeRoundImage(context, orderListBean.getBiaoZhunImg() + itemsViewBean.getGoodsImg(), viewHolder.iv_goods_pic,10);
                }
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                viewHolder.ll_pic_tag.setVisibility(View.GONE);
            } else {
                String keywords = itemsViewBean.getKeywords();
                String catName = itemsViewBean.getCatName();
                viewHolder.tv_goods_tel.setText(keywords);
                viewHolder.tv_goods_type.setText(catName);
                viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
//                    Glide.with(context).load(R.mipmap.common_bg).into(iv_goods_pic);
                GlideUtils.loaderHanldeRoundImageResource(context, R.mipmap.common_bg, viewHolder.iv_goods_pic,10);
            }
        } else {
            if (!TextUtils.isEmpty(itemsViewBean.getGoodsImg())) {
                GlideUtils.loaderHanldeRoundImage(context, itemsViewBean.getGoodsImg(),viewHolder. iv_goods_pic,10);
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                viewHolder.ll_pic_tag.setVisibility(View.GONE);
            } else {
                //默认图
//                GlideUtils.loaderImage(context, R.mipmap.common_bg, iv_goods_pic);
                String keywords = itemsViewBean.getKeywords();
                String catName = itemsViewBean.getCatName();
                viewHolder.tv_goods_tel.setText(keywords);
                viewHolder.tv_goods_type.setText(catName);
                GlideUtils.loaderHanldeRoundImageResource(context, R.mipmap.common_bg,  viewHolder.iv_goods_pic,10);
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
            }

        }


        if (orderListBean.getOrderType() == 5) {

            if (itemsViewBean.getCourseType() == 0) {
//                    courseType = "点播课";
//
//                    默认图片：wwwStatic /img/online_default.jpg
                if (TextUtils.isEmpty(itemsViewBean.getGoodsImg())) {
                    GlideUtils.loaderHanldeRoundImage(context, AppUtils.BASE_URL + "static/img/online_default.jpg",  viewHolder.iv_goods_pic,10);
                } else {
                    GlideUtils.loaderHanldeRoundImage(context, orderListBean.getFileServer() + itemsViewBean.getGoodsImg(), viewHolder. iv_goods_pic,10);
                }


            }
            if (itemsViewBean.getCourseType() == 1) {
//                    courseType = "直播课";
//
//                    wwwStatic /img/online_default.jpg
                if (TextUtils.isEmpty(itemsViewBean.getGoodsImg())) {
                    GlideUtils.loaderHanldeRoundImage(context, AppUtils.BASE_URL + "static/img/online_default.jpg", viewHolder. iv_goods_pic,10);
                } else {
                    GlideUtils.loaderHanldeRoundImage(context, orderListBean.getFileServer() + itemsViewBean.getGoodsImg(), viewHolder. iv_goods_pic,10);
                }
            }
            if (itemsViewBean.getCourseType() == 2) {
//                    courseType = "线下课程";
//                    默认图片：wwwStatic /img/offline_default.jpg
                if (TextUtils.isEmpty(itemsViewBean.getGoodsImg())) {
                    GlideUtils.loaderHanldeRoundImage(context, AppUtils.BASE_URL + "static/img/offline_default.jpg",  viewHolder.iv_goods_pic,10);
                } else {
                    GlideUtils.loaderHanldeRoundImage(context, orderListBean.getFileServer() + itemsViewBean.getGoodsImg(),  viewHolder.iv_goods_pic,10);

                }
            }
            if (itemsViewBean.getCourseType() == 3) {
//
//                    默认图片：wwwStatic /img/customized_default.jpg
                if (TextUtils.isEmpty(itemsViewBean.getGoodsImg())) {
                    GlideUtils.loaderHanldeRoundImage(context, AppUtils.BASE_URL + "static/img/customized_default.jpg",  viewHolder.iv_goods_pic,10);
                } else {
                    GlideUtils.loaderHanldeRoundImage(context, orderListBean.getFileServer() + itemsViewBean.getGoodsImg(),  viewHolder.iv_goods_pic,10);

                }
            }


//                if (TextUtils.isEmpty(itemsViewBean.getGoodsImg())){
//
//                    GlideUtils.loaderImage(context, AppUtils.BASE_URL+"static/img/online_default.jpg",iv_goods_pic);
//                }else {
//                    GlideUtils.loaderImage(context,fileServer+itemsViewBean.getGoodsImg(),iv_goods_pic);
//                }
        }


        viewHolder.tv_goods_titls.setText(itemsViewBean.getGoodsName());


        if (orderListBean.getOrderType() != 5) {

            String subtitle = orderSubtitle(itemsViewBean.getSkuName());
            if (subtitle != null) {
                viewHolder.tv_goods_subtitle.setText(subtitle);
            }
        } else {
            if (TextUtils.isEmpty(itemsViewBean.getSkuName())) {
                return;
            }
            viewHolder.tv_goods_subtitle.setText(itemsViewBean.getSkuName());
        }
        if (!TextUtils.isEmpty(itemsViewBean.getAttrShow())) {
            viewHolder. tv_status.setText(itemsViewBean.getAttrShow());
            viewHolder.tv_status.setVisibility(View.VISIBLE);
        }

        viewHolder.tv_pingjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, OrderEvaluateActivity.class);
                intent.putExtra("goods_order", itemsViewBean);
                intent.putExtra("good_bean",orderListBean);

                context.startActivity(intent);
            }
        });


        viewHolder.rl_alone.setVisibility(View.VISIBLE);
    }

    @Override
    public int getItemCount() {
        return itemsView.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_goods_pic;
        private TextView tv_goods_type;
        private TextView tv_goods_tel;
        private LinearLayout ll_pic_tag;
        private TextView tv_status;
        private TextView tv_goods_titls;
        private TextView tv_goods_subtitle;
        private TextView tv_pingjia;
        private RelativeLayout rl_alone;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_goods_pic=itemView.findViewById(R.id.iv_goods_pic);
            tv_goods_type=itemView.findViewById(R.id.tv_goods_type);
            tv_goods_tel=itemView.findViewById(R.id.tv_goods_tel);
            ll_pic_tag=itemView.findViewById(R.id.ll_pic_tag);
            tv_status=itemView.findViewById(R.id.tv_status);
            tv_goods_titls=itemView.findViewById(R.id.tv_goods_titls);
            tv_goods_subtitle=itemView.findViewById(R.id.tv_goods_subtitle);
            tv_pingjia=itemView.findViewById(R.id.tv_pingjia);
            rl_alone=itemView.findViewById(R.id.rl_alone);
        }
    }
    private String orderSubtitle(String orderSkuName) {
        JSONArray jsonArray = null;
        String skuName = "";
        try {
            if (TextUtils.isEmpty(orderSkuName)) {
                return null;
            }
            jsonArray = new JSONArray(orderSkuName);
            StringBuffer paramsSb = new StringBuffer("");
            StringBuffer moreParamsb = new StringBuffer("");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                Iterator<String> keys = jsonObject.keys();
                String key = "";
                while (keys.hasNext()) {//遍历JSONObject

                    key = (String) keys.next().toString();

                    if (null != key && !"".equals(key)) {

                        String value = (String) jsonObject.get(key);
                        if (jsonArray.length()>1){
//                            skuName = value +"; ";

                            paramsSb.append(value + " ; ");
                        }else {
                            paramsSb.append(value);
                        }

                    }

                }


            }
            if (jsonArray.length()>1){
                moreParamsb.append(paramsSb.deleteCharAt(paramsSb.length() - 2));
                return moreParamsb.toString();
            }else {
                return paramsSb.toString();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
